package com.example.loggingservice.consumer;

import com.example.loggingservice.mapper.OperationLogMapper;
import com.example.loggingservice.model.OperationLog;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * 功能描述:
 *
 * @author Lenovo
 * @date 2025/6/15
 */
@Slf4j
@Component
public class OperationLogConsumer {
    @Autowired
    private OperationLogMapper operationLogMapper;

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(value = "operation-log-queue", durable = "true"),
            exchange = @Exchange(value = "operation-log-exchange", type = "topic"),
            key = "operation.log"
    ))
    public void handleOperationLog(OperationLog event) {
        try {
            log.info("接收到操作日志消息: userId={}, action={}, ip={}",
                    event.getUserId(), event.getAction(), event.getIp());
            operationLogMapper.insert(event);
            log.info("操作日志保存成功: logId={}", event.getLogId());
        } catch (Exception e) {
            log.error("处理操作日志消息失败", e);
        }
    }
}
